package ups.MapParse;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;

public class ShipNameFinder {
	/* Class Variables */
	private static Hashtable<String,Image> patterns = null;
	
	/* Member Variables */
	public ArrayList<String> shipNames;
	
	/* Constructors */
	public ShipNameFinder( Image mapImage ) {
		if( patterns == null ) {
			patterns = _loadPatterns();
		}
		
		shipNames = new ArrayList<String>();
		
		int X = 33;
		int Y;
		
		for( Y = 0; Y < mapImage.getSize().height - 10; Y++ ) {
			for( X = 33; X < 36; X++) {
			//System.out.println( Y );
			for( String name : patterns.keySet() ) {
			//	System.out.println( name );
				//System.out.println("Checking " + name + " at " + new Point( X, Y ).toString());
				Image pattern = patterns.get( name );
				Point targetPoint = new Point( X, Y );
				if( pattern.matchesRegion( mapImage , new Rect( targetPoint, pattern.getSize() ))) {
					shipNames.add( name );
					//System.out.println( "\t\tFOUND !!!!! " + name );
					Y += pattern.getSize().height - 1;
				}
			}
		}
	}
	}
	
	/* Accessors */
	public ArrayList<String> getShipNames() {
		return shipNames;
	}
	/* Utility */
	
	private Hashtable<String,Image> _loadPatterns() {
		Hashtable<String,Image> ht = new Hashtable<String,Image>();
		String folderName = "/Users/scanfield/Desktop/ShipNames/";
		File folder = new File( folderName );
		
		File[] children = folder.listFiles();
		for( File item : children ) {
			try {
				String name = item.getName().substring( 0, item.getName().length() - 4 );
				if( item.getName().substring( item.getName().length() - 3 ).equals( "png")) {
					//System.out.println(name);
					Image img = new Image( item );
					ht.put( name, img );
				}
			} catch (Exception e) {
				System.out.println("Couldn't load image.");
				e.printStackTrace();
			}
		}
		
		return ht;
	}
}
